package project.dade.cn.bst.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.InjectView;
import cn.bingoogolapple.androidcommon.adapter.BGAOnItemChildClickListener;
import cn.bingoogolapple.androidcommon.adapter.BGAOnRVItemClickListener;
import project.dade.cn.bst.R;
import project.dade.cn.bst.adapter.base.MyfeedsViewAdapter;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.manager.AppApplication;
import project.dade.cn.bst.model.BaseRequestsModel;
import project.dade.cn.bst.model.MyfeedsDate;
import project.dade.cn.bst.network.FormatRequest;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.AppUtil;
import project.dade.cn.bst.utils.Divider;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.views.EmptyLayout;

/**
 * Created by vain zhou on 2016/1/13.
 */
public class MyFeedActivity extends ZJPBaseBarActivity implements View.OnClickListener, BGAOnRVItemClickListener ,BGAOnItemChildClickListener {
    @InjectView(R.id.baseListView)
    RecyclerView recyclerView;
    @InjectView(R.id.error_layout)
    EmptyLayout mErrorLayout;
    private MyfeedsViewAdapter myfeedsViewAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myfeeds);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        myfeedsViewAdapter = new MyfeedsViewAdapter( recyclerView);
        myfeedsViewAdapter.setOnRVItemClickListener(this);
        recyclerView.addItemDecoration(new Divider(this));
        recyclerView.setAdapter(myfeedsViewAdapter);
        myfeedsViewAdapter.setOnRVItemClickListener(this);
        myfeedsViewAdapter.setOnItemChildClickListener(this);
    }
    @Override
    protected void onPostResume() {
        super.onPostResume();
        initRequest();

    }

    private void initRequest() {
        if (AppUtil.isNetworkAvailable(this)) {
            requestGetAddress();
        } else {
            mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }

    }

    private void requestGetAddress() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("UID", AppApplication.getInstance().getSpUtil().getUserID().toString());
        hashMap.put("MobilePhone", AppApplication.getInstance().getSpUtil().getUSER_mobile());
        Map<String, String> params = new HashMap<String, String>();
        params.put("UID", AppApplication.getInstance().getSpUtil().getUserID().toString());
        params.put("MobilePhone", AppApplication.getInstance().getSpUtil().getUSER_mobile());
        params.put("Sign", FormatRequest.main(hashMap));
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.QueryCollection).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {

                        ToastUtil.showShort(MyFeedActivity.this, "网络相应失败请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {
                        LogUtil.i(TAG, "获取收藏商品：" + response);
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取收藏商品"))) {
                            BaseRequestsModel model = JSON.parseObject(response, BaseRequestsModel.class);
                            LogUtil.i(TAG, "获取收藏商品：" + model.getData());
                            //获取请求成功
                            List<MyfeedsDate> myfeedsDates = new ArrayList<>();
                            myfeedsDates = JSON.parseArray(model.getData(), MyfeedsDate.class);

                            LogUtil.i(TAG, "获取收藏商品：" + myfeedsDates);
                            myfeedsViewAdapter.setDatas(myfeedsDates);
                            if (myfeedsDates.size() <= 0) {
                                mErrorLayout.setErrorType(EmptyLayout.NODATA);
                                mErrorLayout.setErrorMessage("暂无内容");
                            }
                        } else {
                            ToastUtil.showShort(MyFeedActivity.this, NetRequestUtil.requestsError(response, "获取收藏"));

                        }
                    }

                });
    }

    @Override
    public void onRVItemClick(ViewGroup viewGroup, View view, int position) {
        Intent mIntentRegInfo = new Intent(this, ProductDtailActivity.class);
        mIntentRegInfo.putExtra("id", myfeedsViewAdapter.getItem(position).getProductId());
        startActivity(mIntentRegInfo);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemChildClick(ViewGroup viewGroup, View childView, final int position) {
        if (childView.getId() == R.id.iv_dustbean) {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("UID", AppApplication.getInstance().getSpUtil().getUserID().toString());
            params.put("MobilePhone",AppApplication.getInstance().getSpUtil().getUSER_mobile().toString());
            params.put("ProductId", myfeedsViewAdapter.getItem(position).getProductId());
            params.put("Id",myfeedsViewAdapter.getItem(position).getId());
            params.put("Sign", FormatRequest.main(params));
            new OkHttpRequest.Builder()
                    .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.Collection).params(params)
                    .post(new ResultCallback<String>() {
                        @Override
                        public void onError(Request request, Exception e) {

                            ToastUtil.showShort(MyFeedActivity.this, "网络相应失败请稍后再试");
                        }

                        @Override
                        public void onResponse(String response) {
                            LogUtil.i(TAG, "删除收藏：" + response);
                            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                           BaseRequestsModel model = JSON.parseObject(response, BaseRequestsModel.class);
                            if (model != null && model.getResult() == 0) {

                               myfeedsViewAdapter.removeItem(position);
                            } else {

                                ToastUtil.showShort(MyFeedActivity.this, NetRequestUtil.requestsError(response, "删除收藏地址"));
                            }


                        }

                    });
        }

    }
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
